<template>
  <el-form label-width="100px" ref="form" :model="chanel" :rules="rules">
    <el-form-item label="类型" prop="type">
      <el-select v-model="chanel.type"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.transfer_type"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="协议" prop="protocol">
      <el-select v-model="chanel.protocol"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.collection_protocol"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="端口" prop="com">
       <el-select v-model="chanel.com"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.com"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="波特率" prop="bps">
      <el-select v-model="chanel.bps"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.bps"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="停止位" prop="stop_bit">
      <el-select v-model="chanel.stop_bit"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.stop_bit"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="数据位" prop="data_bit">
      <el-select v-model="chanel.data_bit"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.data_bit"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="校验位" prop="check_bit">
      <el-select v-model="chanel.check_bit"  class="width-100">
        <el-option 
          v-for="item in SYS_PARAMS.check_bit"
          :key="item.name+item.name"
          :label="item.name"
          :value="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="超市周期(ms)" prop="cycle">
        <el-input v-model="chanel.cycle"></el-input>
    </el-form-item>
    <div class="form-button">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm" :loading="loading">{{ loading ? '提交中 ...' : '确 定' }}</el-button>
    </div>
  </el-form>
</template>
<script>
import {mapGetters} from 'vuex';

import {
  addChanelApi,
  updateChanelApi
} from '@/api/setting'

export default {
  name: 'form-chanel',
  props:{
    data: Object
  },
  computed: {
    ...mapGetters(['SYS_PARAMS'])
  },
  watch:{
    data:{
      immediate: true,
      handler(){
        this.chanel = this.data;
        console.log(this.chanel)
      }
    }
  },
  data(){
    return {
      chanel: {
        type: '网口',
        protocol: 'modbus',
        com: 'com1',
        bps: '9600',
        data_bit: '8',
        stop_bit: '1',
        check_bit: 'N',
        cycle: '500',
      },
      rules: {

      },
      loading: false,
    }
  },
  methods: {
    cancel(){
      this.$emit('close')
    },
    confirm(){
      this.$refs.form.validate( valid => {
        if(valid){
          this.loading = true;
          if(this.chanel.id) this.updatechanel();
          else this.addChanel();
        }
      })
    },
    addChanel(){
      addChanelApi(this.chanel).then(res => {
        this.loading = false;
        if(res.code == 0){
          this.$message({type: 'success', message: '提交成功'})
          this.$emit('refresh', {parent_id: this.chanel.parent_id})
          this.$emit('close')
        }
      })
    },
    updateChanel(){
      updateChanelApi(this.chanel).then(res => {
        this.loading = false;
        if(res.code == 0){
          this.$message({type: 'success', message: '提交成功'})
          this.$emit('refresh', {parent_id: this.chanel.parent_id})
          this.$emit('close')
        }
      })
    },
  }

}
</script>
<style  scoped>
.form-button{
  margin-left: 30px;
  position: absolute;
  bottom: 20px;
}
</style>
